package com.example.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.blog.domain.Communication;
import com.example.blog.domain.Response;
import com.example.blog.note.LogInfo;

import com.example.blog.note.NoToken;
import com.example.blog.service.impl.CommunicationServiceImpl;
import com.example.blog.utils.RedisStorage;
import com.example.blog.dto.comments.CommunicationSendDto;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Transactional
@Slf4j
@RequestMapping("/communication")
public class CommunicationController extends BaseController {
    private final RedisStorage redisStorage;
    private final CommunicationServiceImpl communicationService;

    @Autowired
    public CommunicationController(CommunicationServiceImpl communicationService, RedisStorage redisStorage) {
        this.communicationService = communicationService;
        this.redisStorage = redisStorage;
    }

    ;

    //获取即时的信息
    @GetMapping("/infoGet/{id}")
    @LogInfo("获取即时的信息")
    public Response communicationGet(@PathVariable("id") long id) throws JsonProcessingException {
        List<Communication> communications;
        communications = redisStorage.communicationGet(getLogSign().getUid());
        communicationService.saveBatch(communications); //持久化到数据库
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return new Response(Response.SUCCEED_CODE, "获取即时的信息", communications);

    }


    //发送消息
    @NoToken
    @LogInfo("发送消息")
    @PostMapping("/infoSet")
    public Response communicationSet(@RequestBody List<CommunicationSendDto> communications) throws JsonProcessingException {
        if (communications.size() > 1) {
            communications.forEach(s -> {
                s.setSender(getLogSign().getUid());
            });
        }
        return new Response(Response.SUCCEED_CODE, "发送消息", redisStorage.communicationSet(communications));
    }


    //用户获取所有的信息
    @LogInfo("用户获取所有的信息")
    @GetMapping("/userInfo")
    public Response communicationUserGetAll() {
        LambdaQueryWrapper<Communication> wrapper = new LambdaQueryWrapper<Communication>();
        wrapper.ne(Communication::getState, -1)
                .eq(Communication::getRecipient, getLogSign().getUid())
                .orderByDesc(Communication::getDateTime);
        return new Response(
                Response.SUCCEED_CODE,
                "用户获取所有的信息",
                communicationService.list(wrapper)
        );
    }

    //消息未读数
    @LogInfo("消息未读数")
    @GetMapping("/messageReadSum")
    public Response messageNum() {
        QueryWrapper<Communication> wrapper = new QueryWrapper<>();
        wrapper.select("COUNT(*) as length").eq("recipient", getLogSign().getUid()).eq("state", 0);
        Map<String, Object> list = communicationService.getMap(wrapper);
        return new Response(Response.SUCCEED_CODE, "消息未读数", list.get("length"));
    }


    //消息已读
    @LogInfo("消息已读,更改消息状态")
    @PutMapping("/messageRead/{id}")
    public Response messageRead(@PathVariable("id") long id) {
        UpdateWrapper<Communication> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id).setSql("state = 1");
        return new Response(Response.SUCCEED_CODE, "修改成功", (Boolean) communicationService.update(wrapper));
    }


    //删除信息
    @LogInfo("删除信息")
    @DeleteMapping("/messageDel/{id}")
    public Response messageDel(@PathVariable("id") long id) {
        LambdaQueryWrapper<Communication> cw = new LambdaQueryWrapper<>();
        Communication one = communicationService.getOne(cw.eq(Communication::getId, id));
        UpdateWrapper<Communication> wrapper = new UpdateWrapper<>();
        if (one.getRecipient() == getLogSign().getUid()) {
            wrapper.eq("id", id).set("state", -1);
            return new Response(Response.SUCCEED_CODE, "删除成功", (Boolean) communicationService.update(null, wrapper));
        }
        return new Response(Response.ERROR_CODE, "删除失败");
    }
}

